package org.qdao.core;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 
 * @author 譚元吉
 * @since 2010/02/04 14:38:43
 */
public class JarClassLoader extends URLClassLoader {

	public JarClassLoader(String[] classpaths) {
		super(convert(classpaths), JarClassLoader.class.getClassLoader());
	}

	public JarClassLoader(String[] classpaths, ClassLoader parent) {
		super(convert(classpaths), parent);
	}

	public static URL[] convert(String[] classpaths) {
		URL[] urls = new URL[classpaths.length];
		int count = 0;
		for (int i = 0; i < classpaths.length; i++) {
			try {
				URI uri = new File(classpaths[i]).toURI();
				urls[i] = uri.toURL();
				++count;
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		URL[] result = new URL[count];
		System.arraycopy(urls, 0, result, 0, count);
		return result;
	}

	public static void main(String args[]) throws Exception {
/*		JarClassLoader loader = new JarClassLoader(new String[] { "D:\\JAR\\frame.jar"
				, "D:\\Designer-Eclipse\\workspace\\WizardDemo\\bin\\com\\tan\\window\\MyApplicationWindow.class"
				});
		JarFile jarFile = new JarFile(new File("d:\\JAR\\frame.jar"));
		Enumeration<JarEntry> enumerations = jarFile.entries();
		while (enumerations.hasMoreElements()) {
			JarEntry jarEntry = (JarEntry) enumerations.nextElement();
			String oldName = jarEntry.getName();
			String newName = oldName.replaceFirst(".class", "").replace('/', '.');
			System.out.println(newName);
			if ("com.tan.util.SWTFrame".equals(newName)) {
				Class<?> clazz = loader.loadClass(newName);
				System.out.println("the class is loader " + clazz);
			}
		}*/
	}
}
